package com.akk.business;

import com.akk.common.enums.Enums;
import com.akk.module.avs.entity.AvsCustomer;
import com.akk.module.avs.model.request.GoInAvsRequest;
import com.akk.module.avs.model.response.MemberResponse;
import com.akk.module.avs.service.AvsCustomerService;
import com.akk.module.hlbpay.util.HlbBeanUtils;
import com.akk.module.hxt.utils.HttpClientUtilNew;
import com.akk.utils.IDUtils;
import com.akk.utils.RestResponse;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
public class AvsCustomerBusiness {


    @Autowired
    AvsCustomerService avsCustomerService;

    public RestResponse goInAvs(GoInAvsRequest request) throws Exception {
//        return goInAvs2(request);
        if (request.getIsReal().intValue() != Enums.RealAuthEnum.SUCCESS.getValue())
            return RestResponse.build(RestResponse.AVS_ERROR_CODE, "用户未完成实名");
        List<AvsCustomer> customers = avsCustomerService.findByUniqueIdAndAppId(request.getUserId(), request.getAppId());
        Map<String, String> result = new HashMap<>();
        if (customers.isEmpty()) {
            AvsCustomer customer = new AvsCustomer();
            customer.setAppId(request.getAppId());
            customer.setIdCard(request.getIdCard());
            customer.setCustomerId(IDUtils.getPK());
            customer.setIsReal(request.getIsReal());
            customer.setCustomerName(request.getUsername());
            customer.setPhone(request.getPhone());
            customer.setUniqueId(request.getUserId());
            customer.setLoginDate(new Date());
            customer.setAppName(request.getAppName());
//            customer.setHkFileUpload(0);
            avsCustomerService.insert(customer);
            result.put("customerId", customer.getCustomerId());
        } else {
            AvsCustomer customer = customers.get(0);
            if (!request.getAppName().equals(customer.getAppName())
                    || !request.getPhone().equals(customer.getPhone())
                    || !request.getUsername().equals(customer.getCustomerName())
                    || new Date().getTime() - customer.getLoginDate().getTime() > 5000) {
                customer.setAppName(request.getAppName());
                customer.setPhone(request.getPhone());
                customer.setCustomerName(request.getUsername());
                customer.setLoginDate(new Date());
                avsCustomerService.update(customer);
                result.put("customerId", customer.getCustomerId());
            }
        }
        return RestResponse.build(result);
    }


    public RestResponse goInAvs2(GoInAvsRequest request) throws Exception {
        Map<String, String> result = new HashMap<>();
        Map params = HlbBeanUtils.convertBean(request, new TreeMap());
        String res = HttpClientUtilNew.httpPostRequest("https://akk.028wkf.cn/gateway/webapp/userLoginRes", params);
//        String res = HttpClientUtilNew.httpPostRequest("http://127.0.0.1:7777/userLoginRes", params);
        MemberResponse response = new Gson().fromJson(res, MemberResponse.class);
        result.put("customerId", response.getMemberId());
        return RestResponse.build(result);
    }
}
